package org.zhouhe.web.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.zhouhe.web.tag.function.FmtBodyFunction;
import org.zhouhe.web.tag.function.FmtDateFunction;
import org.zhouhe.web.tag.function.FmtNumberFunction;

public class Column extends BodyTagSupport {
	private static final long serialVersionUID = 1L;
	private String field;
	private String label;
	private String width;
	private String align;
	private String formatter;
	private String pattern;
	private String tagBody;
	
	private SimplePage parentTag ;
    @Override  
    public int doStartTag() throws JspException {
    	this.tagBody = null;
    	parentTag = (SimplePage) this.getParent().getParent().getParent();
        return super.doStartTag();
    } 
    
    @Override  
    public void doInitBody() throws JspException {  
        super.doInitBody();  
    }  
    
	@Override
	public int doAfterBody() throws JspException {
		try {
			BodyContent bodycontent = getBodyContent(); 
			System.out.println("================="+bodycontent.getString());
			this.tagBody = bodycontent.getString();
			try {
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

    @Override  
    public int doEndTag() throws JspException {
		String defaultFormatter = "fmt_"+ parentTag.getId() +"_grid_"+ field;
		System.out.println("=================E:"+pattern);
		StringBuilder sb = new StringBuilder("<th ");
		//field:
		sb.append(" field='"+ field +"' ");
		//width:
		if(width != null){
			sb.append(" width='"+ width +"' ");
		}
		//align:
		if(align != null){
			sb.append(" align='"+ align +"' ");
		}
		//formatters
		if(formatter != null){// user defined formatter:
			sb.append(" formatter='"+ formatter +"' ");
		} else if(pattern != null){// generate system default formatter by pattern: 
			String[] tmp = pattern.split(",");
			if(tmp.length==2){
				if("date".equalsIgnoreCase(tmp[0].trim())){//generate date formatter:
					sb.append(" formatter='"+ defaultFormatter +"' ");
					parentTag.addFunction(new FmtDateFunction(defaultFormatter, tmp[1].trim()).toString());
				}else if("number".equalsIgnoreCase(tmp[0].trim())){//generate number formatter:
					sb.append(" formatter='"+ defaultFormatter +"' ");
					parentTag.addFunction(new FmtNumberFunction(defaultFormatter, tmp[1].trim()).toString());
				}
			}
		} else if(tagBody != null && tagBody.trim().length() > 0){//generate tagbody formatter:
			sb.append(" formatter='"+ defaultFormatter +"' ");
			parentTag.addFunction(new FmtBodyFunction(defaultFormatter, tagBody).toString());
		} 
		sb.append(">");
		sb.append(label);
		sb.append("</th>");
		try {
			JspWriter out = pageContext.getOut();
			out.write(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
        return super.doEndTag();
    }  
  
	public String getField() {
		return field;
	}

	public void setField(String field) {
		this.field = field;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getFormatter() {
		return formatter;
	}

	public void setFormatter(String formatter) {
		this.formatter = formatter;
	}

	public String getLabel() {
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}
}
